/**
Nectarine Utility Project is a software interface for the web system 'demovibes'
Copyright (C) 2010  Alexandre LAURENT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

website: http://code.google.com/p/nectarine-utility-project/
e-mail: lw.demoscene@gmail.com
**/

#include <QByteArray>
#include <QXmlStreamReader>

#include <QDebug>

#include "artist.h"

#include "../utils.h"

QDebug operator<<(QDebug dbg, const Artist& a)
{
    dbg << "Artist(\n";
    dbg << "Id: " << a.id << "[" << a.flag << "] " <<  " Nick: " << a.nickname << " RealName: " << a.realname << "\n";
    dbg << "Status: " << statusToString(a.status) << "\n";
    dbg << "Town: " << a.location << "\n";
    dbg << "Website: " << a.website << "\n";
    dbg << "Info: " << a.info << "\n";
    dbg << "Twitter: " << a.twitter << "\n";
    dbg << "LastFM: " << a.lastfm << "\n";
    dbg << "Alias of: " << a.aliasOf << "\n";
    dbg << "Aliases: " << a.aliases << "\n";
    dbg << "Groups: " << a.groups << "\n";
    dbg << "Songs: " << a.songs << "\n";
    dbg << ")\n";

    return dbg.space();
}

bool parseArtist(QXmlStreamReader& xmlReader, Artist& a)
{
    QXmlStreamReader::TokenType nodeType = xmlReader.tokenType();
    bool isOk = true;

    qDebug() << "parseArtist";

    while ( (nodeType != QXmlStreamReader::EndElement || xmlReader.name() != "artist") && isOk )
    {
        if ( nodeType == QXmlStreamReader::StartElement )
        {
            if ( xmlReader.name() == "artist" )
            {
                a.id = xmlReader.attributes().value("id").toString().toUInt();
                isOk &= Utils::getXMLStringData(xmlReader,a.nickname);
            }
            else
            {
                qDebug() << "One tag is not parsed at all -> " << xmlReader.name();
            }
        }

        nodeType = xmlReader.readNext();
    }

    qDebug() << a;

    return isOk;
}

bool parseArtistPage(QXmlStreamReader& xmlReader, Artist& a)
{
    QXmlStreamReader::TokenType nodeType = xmlReader.tokenType();
    bool isOk = true;

    qDebug() << "parseArtistPage";

    while ( (nodeType != QXmlStreamReader::EndElement || xmlReader.name() != "artist") && isOk )
    {
        if ( nodeType == QXmlStreamReader::StartElement )
        {
            if ( xmlReader.name() == "artist" )
            {
                a.id = xmlReader.attributes().value("id").toString().toUInt();
            }
            else if ( xmlReader.name() == "handle" )
            {
                isOk &= Utils::getXMLStringData(xmlReader,a.nickname);
            }
            else if ( xmlReader.name() == "status" )
            {
                QString statusString;
                isOk &= Utils::getXMLStringData(xmlReader,statusString);
                if ( isOk )
                {
                    a.status = statusFactory(statusString);
                }
            }
            else if ( xmlReader.name() == "name")
            {
                isOk &= Utils::getXMLStringData(xmlReader,a.realname);
            }
            else if ( xmlReader.name() == "website")
            {
                QString websiteString;
                isOk &= Utils::getXMLStringData(xmlReader,websiteString);
                if ( isOk )
                {
                    a.website = QUrl(websiteString);
                }
            }
            else if ( xmlReader.name() == "country")
            {
                isOk &= Utils::getXMLStringData(xmlReader,a.flag);
            }
            else if ( xmlReader.name() == "location" )
            {
                isOk &= Utils::getXMLStringData(xmlReader,a.location);
            }
            else if ( xmlReader.name() == "info" )
            {
                isOk &= Utils::getXMLStringData(xmlReader,a.info);
            }
            else if ( xmlReader.name() == "twitter" )
            {
                // ToDO : Need test
                isOk &= Utils::getXMLStringData(xmlReader,a.twitter);
            }
            else if ( xmlReader.name() == "lastfm" )
            {
                // ToDO : need test
                isOk &= Utils::getXMLStringData(xmlReader,a.lastfm);
            }
            else if ( xmlReader.name() == "aliasof")
            {
                // Todo : need test ( id 4228 can test it )
                isOk &= Utils::getXMLStringData(xmlReader,a.aliasOf);
            }
            else if ( xmlReader.name() == "aliases" )
            {
                while ( (nodeType != QXmlStreamReader::EndElement || xmlReader.name() != "aliases") && isOk )
                {
                    if ( nodeType == QXmlStreamReader::StartElement && xmlReader.name() == "alias" )
                    {
                        QString alias;
                        isOk &= Utils::getXMLStringData(xmlReader,alias);
                        a.aliases.push_back(alias);
                    }

                    nodeType = xmlReader.readNext();
                }
            }
            else if ( xmlReader.name() == "groups" )
            {
                while ( (nodeType != QXmlStreamReader::EndElement || xmlReader.name() != "groups") && isOk )
                {
                    if ( nodeType == QXmlStreamReader::StartElement && xmlReader.name() == "group" )
                    {
                        Group g;
                        isOk &= parseGroup(xmlReader,g);
                        a.groups.push_back(g);
                    }

                    nodeType = xmlReader.readNext();
                }
            }
            else if ( xmlReader.name() == "songs" )
            {
                while ( (nodeType != QXmlStreamReader::EndElement || xmlReader.name() != "songs") && isOk )
                {
                    if ( nodeType == QXmlStreamReader::StartElement && xmlReader.name() == "song" )
                    {
                        Song s;
                        isOk &= parseSong(xmlReader,s);
                        a.songs.push_back(s);
                    }

                    nodeType = xmlReader.readNext();
                }
            }
            else
            {
                qDebug() << "One tag is not parsed at all -> " << xmlReader.name();
            }
        }

        nodeType = xmlReader.readNext();
    }

    qDebug() << a;

    return isOk;
}

bool readArtistPage(const QByteArray& streamData, Artist& a)
{
    QXmlStreamReader xmlReader(streamData);
    QXmlStreamReader::TokenType nodeType;
    bool isOk = true;

    qDebug() << "readArtistPage";

    while ( !xmlReader.atEnd() )
    {
        nodeType = xmlReader.readNext();

        if ( nodeType == QXmlStreamReader::StartElement && xmlReader.name() == "artist" )
        {
            isOk &= parseArtistPage(xmlReader,a);
        }
    }

    return isOk;
}
